import $API from '@/api/services'
import { isWxBrowser } from '@/utils'
const shareurlSend = location.origin + location.pathname

const handdraw = {
  state: {
    handdrawData: null,
    handOptionId: '',
    handPointId: '',
  },
  actions: {
    // 获取用户数据
    sendCustomInfoHanddraw({ commit }, userData) {
      $API('getCustomInfoOvv', [userData])
    },
    // 获取手绘图数据
    fetchHanddraw({ commit }, _id) {
      $API('getOverview', [_id]).then(res => {
        if (res.code == 200 || res.code == 100) {
          console.log(res.data,"2");
          document.title = res.data.name
          // 动画数据
          let infoLoad = null
          if (res.data.load_type == 1 && res.data.fk_userpano_load_id) {
            infoLoad = {
              fk_userpano_load_id: res.data.fk_userpano_load_id,
              load_type: res.data.load_type,
            }
          } else {
            if (res.data.load) {
              res.data.load.pc_load_img = res.data.load.full_pc_load_img
              res.data.load.load_img = res.data.load.full_load_img
              res.data.loadinfo = res.data.load
              infoLoad = res.data.loadinfo
              infoLoad.load_type = res.data.load_type
              infoLoad.fk_userpano_load_id = res.data.fk_userpano_load_id
            }
          }
          this.dispatch('fetchOverviewLoadInfo', infoLoad)

          commit('SAVE_DATA', res.data)
          if (res.data.overview_menu_items && res.data.overview_menu_items.options && res.data.overview_menu_items.options.length > 0) {
            commit('SAVE_OPTION_ID', res.data.overview_menu_items.options[0].id)
          }
        } else {
          window.$VM.$vgo.tip(res.msg, 'warning')
        }
      })
    },
    // 切换选项卡
    updateHandOptionId({ commit }, id) {
      commit('SAVE_OPTION_ID', id)
    },
    // 热点id更新
    updateHandPointId({ commit }, id) {
      commit('SAVE_POINT_ID', id)
    },
    // 微信分享初始化
    getHanddrawWXShareData({ commit }) {
      if (!isWxBrowser()) return

      let handdrawData = this.getters.handdrawData
      if (!handdrawData) return
      let shareurl = window.location.href
      $API('getOverviewWXShare', [shareurlSend]).then(res => {
        let data = res.data
        if(data) {
          wx.config({
            debug: false,
            appId: data.appId,
            timestamp: data.timestamp,
            nonceStr: data.nonceStr,
            signature: data.signature,
            jsApiList: [
              'onMenuShareTimeline',
              'openLocation',
              'getLocation',
              'onMenuShareAppMessage',
              'onMenuShareQQ',
              'onMenuShareQZone',
              'onMenuShareWeibo',
              'showOptionMenu'
            ]
          })

          let shareContent = {
            title: handdrawData.name,
            desc: '　',
            link: shareurl,
            imgUrl: $globalconfig.URLS.API + handdrawData.smallimageurl,
            success: function() {},
            cancel: function() {}
          }

          window.wx.ready(function() {
            wx.showOptionMenu()

            wx.onMenuShareTimeline(shareContent)

            //分享给朋友
            wx.onMenuShareAppMessage(shareContent)

            //QQ
            wx.onMenuShareQQ(shareContent)

            //QQ微博
            wx.onMenuShareWeibo(shareContent)

            //QQ空间
            wx.onMenuShareQZone(shareContent)

            window.$VM.$vgo.emit('listenWXLoad')
          })

          window.wx.error(function(res) {
            console.log(res)
          })
        }
      })
    }
  },
  mutations: {
    SAVE_DATA: (state, data) => {
      state.handdrawData = data
    },
    SAVE_OPTION_ID: (state, id) => {
      state.handOptionId = id
    },
    SAVE_POINT_ID: (state, id) => {
      state.handPointId = id
    },
  }
}

export default handdraw
